const express = require("express");
const router = express.Router();
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
const timezone = require("dayjs/plugin/timezone");

dayjs.extend(utc);
dayjs.extend(timezone);

let connectDAO = require("./connect.js");

// 获取道路状况
router.get("/get_road_conditions", function (req, res) {
  const sql = `SELECT * FROM Rold`;
  connectDAO
    .queryDataBySQL(sql, (records) => {
      if (records.length > 0) {
        res.send({
          code: 200,
          msg: "Success",
          records,
          total: records.length,
        });
      } else {
        res.send({
          code: 200,
          msg: "no data",
          records,
        });
      }
    })
    .catch((err) => {
      res.send({ code: 201, msg: "error", err });
    });
});

// 提交道路状况
router.post("/submit_road_conditions", function (req, res) {
  const { CoordinateSet, Status, VehiclesNum, Weather } = req.body;
  const sql = `INSERT INTO Rold ( CoordinateSet, Status,VehiclesNum,Weather,CreateTime) VALUES ('${CoordinateSet}', '${Status}', '${VehiclesNum}', '${Weather}', '${dayjs().format(
    "YYYY-MM-DD HH:mm:ss"
  )}')`;
  connectDAO
    .queryDataBySQL(sql)
    .then(() => {
      res.send({ code: 200, msg: "Success" });
    })
    .catch((err) => {
      res.send({ code: 201, msg: "error", err });
    });
});
module.exports = router;
